// pages/mall/mall.js
import {
    getGoodsBySearch
} from "../../services/mallReq";

const {
    getGoodList,
    searchGood
} = require('../../services/mallReq')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        searchList: [],
        searchValue: '',
        // 是否加载完毕
        isFinish: false,
        queryInfo: {
            page: 1,
            limit: 10
        },
        searchInfo: {
            page: 1,
            limit: 10
        }
    },

    onShow() {
        this.setData({
            list: [],
            searchList: [],
        })
        this.handleGetGood()
    },

    onReachBottom: function () {
        if (!this.data.isFinish && this.data.searchValue === '') {
            let queryObj = {
                page: this.data.queryInfo.page + 1,
                limit: this.data.queryInfo.limit
            }
            this.setData({
                queryInfo: queryObj
            })
            this.handleGetGood()
        }

        if (this.data.searchValue !== '') {
            let searchObj = {
                page: this.data.searchInfo.page + 1,
                limit: this.data.searchInfo.limit
            }
        }
    },


    // 获取全部商品
    handleGetGood() {
        wx.showLoading({
            title: '加载中',
        })
        getGoodList(this.data.queryInfo.page, this.data.queryInfo.limit, res => {
            this.setData({
                list: this.data.list.concat(res.data.data.list)
            })
            if (res.data.data.list.length < this.data.queryInfo.limit) {
                this.setData({
                    isFinish: true
                })
            }
            wx.hideLoading();
        })
    },

    // 执行搜索
    handleSearch() {
        wx.showLoading({
            title: '加载中',
        })
        this.setData({
            list: [],
            searchList: [],
        })
        getGoodsBySearch(this.data.searchValue, this.data.searchInfo.page, this.data.searchInfo.limit, res => {
            this.setData({
                searchList: this.data.searchList.concat(res.data.data.list)
            })
            if (res.data.data.list.length < this.data.queryInfo.limit) {
                this.setData({
                    isFinish: true
                })
            }
            wx.hideLoading();
        })
    },
    handleGetInfoGoods(e) {
        const id = e.currentTarget.dataset.goodsid
        wx.navigateTo({
            url: '../mallDetail/mallDetail?id=' + id,
        })
    },
    handlePay(e) {
        console.log(e)

    },
    // 搜索框内容改变
    onChange(e) {
        this.setData({
            searchValue: e.detail
        })
    }
})